﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GiaoDien
{
    public partial class FormThongTinSach : Form
    {
        private DataSet dataset;
        private string sqlquery;
        public FormThongTinSach()
        {
            InitializeComponent();
        }

        private void init()
        {
            dataset = new DataSet();
            DataTable datatabletls = new DataTable();
            dataset.Tables.Add(datatabletls);
            dataset.Tables[0].TableName = "theloaisach";
            DataTable datatabletg = new DataTable();
            dataset.Tables.Add(datatabletg);
            dataset.Tables[1].TableName = "tacgia";
            DataTable datatablenxb = new DataTable();
            dataset.Tables.Add(datatablenxb);
            dataset.Tables[2].TableName = "nhaxuatban";
            DataTable datatablenn = new DataTable();
            dataset.Tables.Add(datatablenn);
            dataset.Tables[3].TableName = "ngonngu";
            DataTable datatables = new DataTable();
            dataset.Tables.Add(datatables);
            dataset.Tables[4].TableName = "sach";
        }

        private void Loadtheloai()
        {
            THELOAISACHBUS tlBUS = new THELOAISACHBUS();
            DataTable data = tlBUS.Loadtheloaisach();
            dataset.Tables["theloaisach"].Clear();
            dataset.Tables["theloaisach"].Merge(data);
            cbbtheloai.DataSource = dataset.Tables["theloaisach"];
            cbbtheloai.DisplayMember = "TenTheLoai";
            cbbtheloai.ValueMember = "MaLoaiSach";
            if (dataset.Tables["theloaisach"].Rows.Count != 0)
                cbbtheloai.SelectedIndex = 0;
        }

        private void Loadtacgia()
        {
            TACGIABUS tgBUS = new TACGIABUS();
            DataTable data = tgBUS.Loadtacgia();
            dataset.Tables["tacgia"].Clear();
            dataset.Tables["tacgia"].Merge(data);
            if (dataset.Tables["tacgia"].Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                txbtacgia.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                txbtacgia.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = dataset.Tables["tacgia"].CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["TenTacGia"].ToString());
                }
                txbtacgia.AutoCompleteCustomSource = auto;
            }
        }

        private void Loadnhaxuatban()
        {
            NHAXUATBANBUS nnxbBUS = new NHAXUATBANBUS();
            DataTable data = nnxbBUS.Loadnhaxuatban();
            dataset.Tables["nhaxuatban"].Clear();
            dataset.Tables["nhaxuatban"].Merge(data);
            cbbnhaxuatban.DataSource = dataset.Tables["nhaxuatban"];
            cbbnhaxuatban.DisplayMember = "TenNhaXuatBan";
            cbbnhaxuatban.ValueMember = "MaNhaXuatBan";
            if (dataset.Tables["nhaxuatban"].Rows.Count != 0)
                cbbnhaxuatban.SelectedIndex = 0;
        }

        private void Loadngongu()
        {
            NGONNGUBUS nnBUS = new NGONNGUBUS();
            DataTable data = nnBUS.Bangngonngu();
            dataset.Tables["ngonngu"].Clear();
            dataset.Tables["ngonngu"].Merge(data);
            cbbngongu.DataSource = dataset.Tables["ngonngu"];
            cbbngongu.DisplayMember = "TenNgonNgu";
            cbbngongu.ValueMember = "MaNgonNgu";
            if (dataset.Tables["ngonngu"].Rows.Count != 0)
                cbbngongu.SelectedIndex = 0;
        }

        private void Loadnamxuatban()
        {
            int yearstart = DateTime.Now.Year - 8;
            for (int i = yearstart; i <= DateTime.Now.Year; i++)
                cbbnamxuatban.Items.Add(i);
            cbbnamxuatban.SelectedIndex = 0;
        }

        private void Loadlantaiban()
        {
            for (int i = 1; i <= 20; i++)
                cbblantaiban.Items.Add(i);
            cbblantaiban.SelectedIndex = 0;
        }

        private void Loadngaynhap()
        {
            for (int i = 1; i <= 31; i++)
                cbbngaynhap.Items.Add(i);
            cbbngaynhap.SelectedIndex = 0;
        }

        private void Loadthangnhap()
        {
            for (int i = 1; i <= 12; i++)
                cbbthangnhap.Items.Add(i);
            cbbthangnhap.SelectedIndex = 0;
        }

        private void Loadnamnhap()
        {
            int yearstart = DateTime.Now.Year - 8;
            for (int i = yearstart; i <= DateTime.Now.Year; i++)
                cbbnamnhap.Items.Add(i);
            cbbnamnhap.SelectedIndex = 0;
        }

        private void Loadsach()
        {
            SACHBUS sBUS = new SACHBUS();
            DataTable data = sBUS.Bangsach();
            dataset.Tables["sach"].Clear();
            dataset.Tables["sach"].Merge(data);
            if (dataset.Tables["sach"].Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                txbtensach.AutoCompleteMode = AutoCompleteMode.Append;
                txbtensach.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = dataset.Tables["sach"].CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["TenSach"].ToString());
                }
                txbtensach.AutoCompleteCustomSource = auto;
            }
        }

        private void Loadquyenso()
        {
            for (int i = 1; i <= 20; i++)
            {
                cbbquyenso.Items.Add(i);
            }
            cbbquyenso.SelectedIndex = 0;
        }

        private void FormThongTinSach_Load(object sender, EventArgs e)
        {
            try
            {
                init();
                Loadtheloai();
                Loadtacgia();
                Loadnamxuatban();
                Loadnhaxuatban();
                Loadlantaiban();
                Loadngongu();
                Loadngaynhap();
                Loadthangnhap();
                Loadnamnhap();
                Loadsach();
                Loadquyenso();
                xemtatca();
                ckbsachmoi.Checked = true;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void masach()
        {
            string theloai = cbbtheloai.SelectedValue.ToString();
            string tensach = txbtensach.Text.Trim().ToUpper();
            string quyenso = cbbquyenso.SelectedItem.ToString();
            string tacgia = txbtacgia.Text.Trim().ToUpper();
            if (theloai != "" && tensach != "" && quyenso != "" && tacgia != "")
            {
                TACGIABUS tgBUS = new TACGIABUS();
                int matacgia = tgBUS.laymatacgia(tacgia);
                if (matacgia == 0)
                    tgBUS.Themtacgia(tacgia);
                matacgia = tgBUS.laymatacgia(tacgia);
                tacgia = matacgia.ToString();
                
                SACHBUS sBUS = new SACHBUS();
                string kq = "";
                if (ckbsachmoi.Checked == true)
                {
                    kq = sBUS.masach(theloai, tensach, tacgia, quyenso, true);
                }
                else
                {
                    kq = sBUS.masach(theloai, tensach, tacgia, quyenso, false);
                    if (kq == "-1")
                        MessageBox.Show("Đã có trong csdl");
                }
                txbmasach.Text = kq;
            }
            else
            {
                MessageBox.Show("thieu thong tin");
            }
        }

        private void tracuumasach(string masach)
        {
            DataView dataview = new DataView(dataset.Tables["sach"]);
            string filter = "MaSach like '%" + masach + "%'";
            dataview.RowFilter = filter;
            dataGridView1.DataSource = dataview;
        }

        private void tracuutensach(string tensach)
        {
            DataView dataview = new DataView(dataset.Tables["sach"]);
            string filter = "TenSach like '%" + tensach + "%'";
            dataview.RowFilter = filter;
            this.dataGridView1.DataSource = dataview;
        }

        private void bttracuu_Click(object sender, EventArgs e)
        {
            if (txbmasach2.Text != "")
                tracuumasach(txbmasach2.Text.Trim());
            else if (txbtensach2.Text != "")
                tracuutensach(txbtensach2.Text.Trim());
        }

        private void xemtatca()
        {
            Loadsach();
            girdtacgia.DataSource = dataset.Tables["tacgia"];
            girdtacgia.DisplayMember = "TenTacGia";
            girdtacgia.ValueMember = "MaTacGia";
            girdtheloai.DataSource = dataset.Tables["theloaisach"];
            girdtheloai.DisplayMember = "TenTheLoai";
            girdtheloai.ValueMember = "MaLoaiSach";
            girdnhaxuatban.DataSource = dataset.Tables["nhaxuatban"];
            girdnhaxuatban.DisplayMember = "TenNhaXuatBan";
            girdnhaxuatban.ValueMember = "MaNhaXuatBan";
            girdngonngu.DataSource = dataset.Tables["ngonngu"];
            girdngonngu.DisplayMember = "TenNgonNgu";
            girdngonngu.ValueMember = "MaNgonNgu";
            this.dataGridView1.DataSource = dataset.Tables["sach"];
        }

        private void btxemtatca_Click(object sender, EventArgs e)
        {
            xemtatca();
        }

        private void capnhat()
        {
            SACHBUS sBUS = new SACHBUS();
            sqlquery = "SELECT * FROM SACH";
            int kq = sBUS.update(sqlquery, dataset);
            if (kq > 0)
                MessageBox.Show("Thành công");
            else
                MessageBox.Show("Thất bại");
        }

        private void btncapnhat_Click(object sender, EventArgs e)
        {
            capnhat();
        }

        private void btnlaymasach_Click(object sender, EventArgs e)
        {
            masach();
        }

        private void btnlapthe_Click(object sender, EventArgs e)
        {
            SACHDTO sDTO = new SACHDTO();
            sDTO.Masach = txbmasach.Text.Trim();
            sDTO.Tensach = txbtensach.Text.Trim().ToUpper();

            TACGIABUS tgBUS = new TACGIABUS();
            string tentacgia = txbtacgia.Text.Trim().ToUpper();
            sDTO.Matacgia = tgBUS.laymatacgia(tentacgia);

            sDTO.Maloaisach = cbbtheloai.SelectedValue.ToString();
            sDTO.Namxuatban = Convert.ToInt32(cbbnamxuatban.SelectedItem.ToString());
            sDTO.Manhaxuatban = Convert.ToInt32(cbbnhaxuatban.SelectedValue.ToString());
            sDTO.Lantaiban =Convert.ToInt32(cbblantaiban.SelectedItem.ToString());

            int ngaynhap = Convert.ToInt32(cbbngaynhap.SelectedItem.ToString());
            int thangnhap = Convert.ToInt32(cbbthangnhap.SelectedItem.ToString());
            int namnhap = Convert.ToInt32(cbbnamnhap.SelectedItem.ToString());
            sDTO.Ngaynhap = new DateTime(namnhap, thangnhap, ngaynhap);

            sDTO.Soluong = Convert.ToInt32(txbsoluong.Text.Trim());
            sDTO.Dongia = Convert.ToDouble(txbdongia.Text.Trim());
            sDTO.Manhanvien = Form1.manhanvien;
            sDTO.Sachquy = "0";
            if (cksachquy.Checked == true)
                sDTO.Sachquy = "1";
            sDTO.Ngonngu = Convert.ToInt32(cbbngongu.SelectedValue.ToString());
            SACHBUS sBUS = new SACHBUS();
            bool kq = sBUS.themsach(sDTO);
            if (kq == true)
            {
                MessageBox.Show("Thêm thành công");
                xemtatca();
            }
            else
                MessageBox.Show("Thêm thất bại");
            
        }

        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DialogResult kq = MessageBox.Show("Bạn chắc không?", "nguyen", MessageBoxButtons.OKCancel);
                if (kq == DialogResult.OK)
                {
                    capnhat();
                }
                else
                {
                    xemtatca();
                }
            }
        }

    }
}
